//
//  WTChatRoomView.m
//  66TV
//
//  Created by ben on 16/8/1.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTChatRoomView.h"
#import "Masonry.h"

#import "WTChatRoomCell.h"
#import "UITableView+FDTemplateLayoutCell.h"


@interface WTChatRoomView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView*  chatRoomTableView;
@property (nonatomic, strong) NSArray<WTBaseMessageModel*>*      dataSource;
@end


@implementation WTChatRoomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}


#pragma mark -
- (void)setup{
    
    
    self.chatRoomTableView = ({
        
        UITableView* view = [[UITableView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        [view registerClass:[WTChatRoomCell class] forCellReuseIdentifier:NSStringFromClass([WTChatRoomCell class])];
        view.separatorStyle = UITableViewCellSeparatorStyleNone;
        view.dataSource = self;
        view.delegate = self;
        [self addSubview:view];
        view;
    });
    
    [self layout];
}

#pragma mark -
- (void)layout{
    
    [self.chatRoomTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.and.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}


#pragma mark -
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WTChatRoomCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WTChatRoomCell class])];
    WTBaseMessageModel* model = self.dataSource[indexPath.row];
    cell.messageModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [tableView fd_heightForCellWithIdentifier:NSStringFromClass([WTChatRoomCell class]) configuration:^(WTChatRoomCell* cell) {
        WTBaseMessageModel* model = self.dataSource[indexPath.row];
        cell.messageModel = model;

    }] + 10;
}


- (void)insert:(NSArray*)messages{
    
    if(!self.dataSource){
        self.dataSource = messages;
        [self.chatRoomTableView reloadData];
    }else {
        NSMutableArray* datas = [[NSMutableArray alloc] initWithArray:self.dataSource];
        if(datas.count > 200){
            [datas removeObjectsInRange:NSMakeRange(0, 10)];
            [datas addObjectsFromArray:messages];
            self.dataSource = [datas copy];
            [self.chatRoomTableView reloadData];
        }else {
            [datas addObjectsFromArray:messages];
            self.dataSource = datas;
            [self.chatRoomTableView reloadData];
        }
    }
     [self.chatRoomTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.dataSource count] - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
@end
